#!/bin/sh
#
# Script to automate BeagleBoard kernel download and build for Lenny.
#
# This script is based on the script update_kernel.sh:
# http://www.rcn-ee.com/deb/kernel/update_kernel.sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License
# http://www.gnu.org/licenses/gpl.txt
# This program is distributed WITHOUT ANY WARRANTY.
#
# $Id$
#############################################################################

if [ $# -ne 3 ]; then
    echo "Usage: `basename $0` <kernel version> <commit> <build>" >&2
    echo >&2
    echo "Example: `basename $0` 2.6.29 58cf2f1 oer41.1" >&2

    exit 1
fi

KERV=$1
COMMIT=$2
BUILD=$3
KERNEL=v${KERV}-${COMMIT}
PATCH_BASE_URL=http://rcn-ee.homeip.net:81/dl/omap/beagle
#PATCH_BASE_URL=http://www.rcn-ee.com/deb/kernel/beagle/lenny

if [ ! -d linux-omap-2.6/ ]; then
    echo "Fetching kernel"
    git-clone git://git2.kernel.org/pub/scm/linux/kernel/git/tmlind/linux-omap-2.6.git
    cd linux-omap-2.6/
else
    cd linux-omap-2.6/
    echo "Updating kernel"
    git pull
fi

echo "Switching to branch ${COMMIT}"
git checkout ${COMMIT} -b ${KERNEL}

echo "Creating kernel archive"
git archive --format=tar --prefix=${KERNEL}/ ${KERNEL} | gzip > ../${KERNEL}.tar.gz

echo "Switching back to master branch"
git checkout master
git branch ${KERNEL} -D
cd ..

echo "Retrieving BeagleBoard patches and config"
wget -c ${PATCH_BASE_URL}/${KERNEL}-${BUILD}/${KERNEL}-${BUILD}.diff
wget -c ${PATCH_BASE_URL}/${KERNEL}-${BUILD}/defconfig

echo "Extracting kernel archive"
tar xf ${KERNEL}.tar.gz
cd ${KERNEL}

echo "Applying patches"
patch -p1 < ../${KERNEL}-${BUILD}.diff

echo "Copying default config"
cp ../defconfig .config
make silentoldconfig

echo "Done. Now you have a kernel in ${KERNEL} to be compiled"


